﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;

namespace ProjetLabo.Controls
{
    /// <summary>
    /// Logique d'interaction pour ControlTable.xaml
    /// </summary>
    public partial class ControlTable : UserControl
    {


        

        public const string STATE_UPDATED = "updated";
        public const string STATE_UPDATING = "updating";
        public const string STATE_NEW = "new";
        public const string STATE_DELETE = "delete";


        public static readonly DependencyProperty TableDependency =
          DependencyProperty.Register("Table", typeof(string),
          typeof(ControlTable), new FrameworkPropertyMetadata("Table"));
 

        private DataTable table;
        private String state;
        private Binding source;
        private int xManager;
        private int yManager;

        public int YManager
        {
            get { return yManager; }
            set { yManager = value; }
        }

        public int XManager
        {
            get { return xManager; }
            set { xManager = value; }
        }
        public String State
        {
            get { return state; }
            set { 
                state = value;
                UpdateView();
            }
        }



        public DataTable Table 
        {
            get { return table; }
            set { 
                table = value;
                
            }
        }

        void blockTName_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            if((string)((TextBox)sender).Tag != ((TextBox)sender).Text)
            {
                if (((TextBox)sender).Name == "Name")
                    this.Table.Columns[(string)((TextBox)sender).Tag].ColumnName = (string)((TextBox)sender).Text;
                if (((TextBox)sender).Name == "Type")
                    this.Table.Columns[(string)((TextBox)sender).Tag].DataType =  System.Type.GetType(((TextBox)sender).Text).GetType();
                this.State = STATE_UPDATING;
                ((TextBox)sender).Tag = ((TextBox)sender).Text;
            }
        }


        public void initialize()
        {
            this.TableTitle.Text = table.TableName;
            this.TableTitle.Tag = table.TableName;
            int i = 0;
            foreach (DataColumn column in table.Columns)
            {
                this.RenderModel.RowDefinitions.Add(new RowDefinition()
                {
                    Height = new GridLength(20)
                });
                TextBox blockTName = new TextBox() { Text = column.ColumnName };
                TextBox blockTType = new TextBox() { Text = column.DataType.Name };

                blockTName.Tag = column.ColumnName;

                blockTName.IsKeyboardFocusedChanged += blockTName_IsKeyboardFocusedChanged;
                blockTType.IsKeyboardFocusedChanged += blockTName_IsKeyboardFocusedChanged;
                blockTName.Initialized += blockTName_Initialized;
                blockTType.Initialized += blockTName_Initialized;
                blockTName.LostKeyboardFocus += blockTName_LostKeyboardFocus;
                blockTType.LostKeyboardFocus += blockTName_LostKeyboardFocus;
                blockTName.Name = "Name";
                blockTType.Name = "Type";

                blockTType.BorderThickness = new Thickness(0);
                blockTName.BorderThickness = new Thickness(0);
                Grid.SetColumn(blockTName, 0);
                Grid.SetColumn(blockTType, 1);
                Grid.SetRow(blockTName, i);
                Grid.SetRow(blockTType, i);
                this.RenderModel.Children.Add(blockTName);
                this.RenderModel.Children.Add(blockTType);
                i++;
            }
        }
        void blockTName_Initialized(object sender, EventArgs e)
        {
            blockTName_IsKeyboardFocusedChanged(sender, new DependencyPropertyChangedEventArgs());
        }

        void blockTName_IsKeyboardFocusedChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            ((TextBox)sender).IsReadOnly = !((TextBox)sender).IsKeyboardFocused;
            if(!((TextBox)sender).IsKeyboardFocused)
            {
                ((TextBox)sender).Background = new SolidColorBrush(Colors.LightGray);
            }
            else
            {
                 ((TextBox)sender).Background = new SolidColorBrush(Colors.White);
            }
        }

        public ControlTable()
        {
            InitializeComponent();
            this.State = STATE_UPDATED;
            this.MouseLeftButtonDown += new MouseButtonEventHandler(Control_MouseLeftButtonDown);
            this.MouseLeftButtonUp += new MouseButtonEventHandler(Control_MouseLeftButtonUp);
            this.MouseMove += new MouseEventHandler(Control_MouseMove);

            System.Windows.Controls.ContextMenu contextmenu = new ContextMenu();
            MenuItem item = new MenuItem();
            item.Header = "ajouter un lien";
            contextmenu.Items.Add(item);
            item.Click += item_Click;
            this.ContextMenu = contextmenu;

            this.table = new DataTable();
        }

        public event Action<object, EventArgs> EventCLique;

        void item_Click(object sender, RoutedEventArgs e)
        {
            EventCLique(this, null);   
        }

        public void UpdateView()
        {
            switch (state)
            {
                case ControlTable.STATE_UPDATED:
                    this.TableView.BorderBrush = new SolidColorBrush(Colors.Green);
                    break;
                case ControlTable.STATE_UPDATING:
                    this.TableView.BorderBrush = new SolidColorBrush(Colors.Orange);
                    break;
                case ControlTable.STATE_NEW:
                    this.TableView.BorderBrush = new SolidColorBrush(Colors.Blue);
                    break;
                case ControlTable.STATE_DELETE:
                    this.TableView.BorderBrush = new SolidColorBrush(Colors.Red);
                    break;
            }
        }




        protected bool isDragging;
        private Point clickPosition;

        private void Control_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            isDragging = true;
            var draggableControl = sender as UserControl;
            clickPosition = e.GetPosition(this);
            draggableControl.CaptureMouse();
        }

        private void Control_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            isDragging = false;
            var draggable = sender as UserControl;
            draggable.ReleaseMouseCapture();
        }

        private void Control_MouseMove(object sender, MouseEventArgs e)
        {
            var draggableControl = sender as UserControl;

            if (isDragging && draggableControl != null)
            {
                Point currentPosition = e.GetPosition(this.Parent as UIElement);

                var transform = draggableControl.RenderTransform as TranslateTransform;
                if (transform == null)
                {
                    transform = new TranslateTransform();
                    draggableControl.RenderTransform = transform;
                }

                transform.X = currentPosition.X - clickPosition.X;
                transform.Y = currentPosition.Y - clickPosition.Y;
            }
        }


        


    }

    
}
